package com.example.day8_sp;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity4 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        requestPermissions(new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_CONTACTS,
                Manifest.permission.READ_SMS,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_CALL_LOG,
                Manifest.permission.READ_CALL_LOG
        },101);
    }

    public void write(View view) {
        //先动态获取权限---上方已执行
        //获取SD卡的状态  MEDIA_MOUNTED相当于正常状态
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //获取根路径
            File file = Environment.getExternalStorageDirectory();
            FileOutputStream stream = null;//有流就要关闭，防止内存泄露内存溢出问题
            //创建输出流
            try {
                stream = new FileOutputStream(new File(file, "2208a.txt"));
                stream.write("2208全员升班".getBytes());//需要转成字节类型
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                if(stream!=null){
                    try {
                        stream.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

        }
    }

    public void load(View view) {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            new MyTask().execute("http://10.161.9.80:8080/res/music/mp3/1405283464.mp3","句号.mp3");
        }
    }

    public void login(View view) {
        new LoginTask().execute("http://10.161.9.80:7003/user/login","zll","123456");
    }

    class MyTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            HttpUtils.download(strings[0],strings[1]);
            return null;
        }
    }
    class LoginTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {

            return HttpUtils.doPost(strings[0],strings[1],strings[2]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s!=""){
                UserEntity entity = new Gson().fromJson(s, UserEntity.class);
                if(entity.getErrorCode()==200){
                    Intent intent = new Intent(MainActivity4.this, MainActivity3.class);
                    startActivity(intent);
                }
            }
        }
    }
}